#include "weldconditiondialog.h"
#include "ui_weldconditiondialog.h"
#include <QDebug>

WeldConditionDialog::WeldConditionDialog(int styleId,CommunicateNormalDelegate *communicateNormalIn,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WeldConditionDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
    communicateNormalDelegate = communicateNormalIn;

}

void WeldConditionDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->returnBtn->setGeometry(500,400,70,40);
        ui->saveBtn->setGeometry(500,350,70,40);
        break;
    default:
        break;
    }
}

WeldConditionDialog::~WeldConditionDialog()
{
    delete ui;
}

void WeldConditionDialog::on_returnBtn_clicked()
{
    this->hide();
}

void WeldConditionDialog::showView_slot()
{
    this->show();
    updateConditionSHow(ui->comboBox->currentIndex());

}

void WeldConditionDialog::updateUiShow(WeldCondition weldCondition){


     ui->lineEdit->setText(QString("%1").arg(weldCondition.voltage));
     ui->lineEdit_2->setText(QString("%1").arg(weldCondition.amp));
     ui->lineEdit_3->setText(QString("%1").arg(weldCondition.weldStartVoltage));
     ui->lineEdit_4->setText(QString("%1").arg(weldCondition.weldStartAmp));
     ui->lineEdit_5->setText(QString("%1").arg(weldCondition.weldEndVoltage));
     ui->lineEdit_6->setText(QString("%1").arg(weldCondition.weldEndAmp));
}

void WeldConditionDialog::on_saveBtn_clicked()
{

    QString str1 = ui->lineEdit->text();
    QString str2 = ui->lineEdit_2->text();
    QString str3 = ui->lineEdit_3->text();
    QString str4 = ui->lineEdit_4->text();
    QString str5 = ui->lineEdit_5->text();
    QString str6 = ui->lineEdit_6->text();

    QString pattern("^[0-9]+(.[0-9]{1,5})?$");
    QRegExp rxRoller(pattern);
    if(!rxRoller.exactMatch(str1)
            || !rxRoller.exactMatch(str2)
            || !rxRoller.exactMatch(str3)
            || !rxRoller.exactMatch(str4)
            || !rxRoller.exactMatch(str5)
            || !rxRoller.exactMatch(str6)){
       emit showErrorMsgBox(QString("存在非法字符，请修改！"));
       return;
    }

    qDebug() << "save";

    int index = ui->comboBox->currentIndex();


    struct WeldCondition weldCondition;
    weldCondition.index = index;
    weldCondition.voltage = ui->lineEdit->text().toDouble();
    weldCondition.amp = ui->lineEdit_2->text().toDouble();
    weldCondition.weldStartVoltage = ui->lineEdit_3->text().toDouble();
    weldCondition.weldStartAmp = ui->lineEdit_4->text().toDouble();
    weldCondition.weldEndVoltage = ui->lineEdit_5->text().toDouble();
    weldCondition.weldEndAmp = ui->lineEdit_6->text().toDouble();

    int returnInfo;
    //gSOAP通信服务端修改信息
    communicateNormalDelegate->changeWeldConditionConfigList(weldCondition,returnInfo);

    emit showErrorMsgBox(QString("保存成功！"));

//    this->hide();
}

void WeldConditionDialog::updateConditionSHow(int index)
{

    std::vector<struct WeldCondition> configListOut;
    if(communicateNormalDelegate->getWeldConditionConfigList(configListOut)==1){
     qDebug() << "listCount:" << configListOut.size();
    }

    struct WeldCondition weldCondition;
    weldCondition = configListOut.at(index);
    updateUiShow(weldCondition);
}

void WeldConditionDialog::on_comboBox_currentIndexChanged(int index)
{
    updateConditionSHow(index);
}
